home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60src.lha / Vim / vim60 / src / quickfix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-27  |  47.8 KB  |  2,028 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * quickfix.c: functions for quickfix mode, using a file with error messages
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. #if defined(FEAT_QUICKFIX) || defined(PROTO)
  17.  
  18. struct dir_stack_T
  19. {
  20.     struct dir_stack_T    *next;
  21.     char_u        *dirname;
  22. };
  23.  
  24. static void    qf_msg __ARGS((void));
  25. static void    qf_free __ARGS((int idx));
  26. static char_u    *qf_types __ARGS((int, int));
  27. static int    qf_get_fnum __ARGS((char_u *, char_u *));
  28. static char_u    *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
  29. static char_u    *qf_pop_dir __ARGS((struct dir_stack_T **));
  30. static char_u    *qf_guess_filepath __ARGS((char_u *));
  31. static void    qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
  32. static void    qf_clean_dir_stack __ARGS((struct dir_stack_T **));
  33. #ifdef FEAT_WINDOWS
  34. static int    qf_win_pos_update __ARGS((int old_qf_index));
  35. static buf_T    *qf_find_buf __ARGS((void));
  36. static void    qf_update_buffer __ARGS((void));
  37. static void    qf_fill_buffer __ARGS((void));
  38. #endif
  39. static char_u    *get_mef_name __ARGS((void));
  40.  
  41. static struct dir_stack_T   *dir_stack = NULL;
  42.  
  43. /*
  44.  * for each error the next struct is allocated and linked in a list
  45.  */
  46. struct qf_line
  47. {
  48.     struct qf_line  *qf_next;    /* pointer to next error in the list */
  49.     struct qf_line  *qf_prev;    /* pointer to previous error in the list */
  50.     linenr_T         qf_lnum;    /* line number where the error occurred */
  51.     int             qf_fnum;    /* file number for the line */
  52.     int             qf_col;    /* column where the error occurred */
  53.     int             qf_nr;    /* error number */
  54.     char_u        *qf_text;    /* description of the error */
  55.     char_u         qf_virt_col; /* set to TRUE if qf_col is screen column */
  56.     char_u         qf_cleared;/* set to TRUE if line has been deleted */
  57.     char_u         qf_type;    /* type of the error (mostly 'E') */
  58.     char_u         qf_valid;    /* valid error message detected */
  59. };
  60.  
  61. /*
  62.  * There is a stack of error lists.
  63.  */
  64. #define LISTCOUNT   10
  65.  
  66. struct qf_list
  67. {
  68.     struct qf_line *qf_start;    /* pointer to the first error */
  69.     struct qf_line *qf_ptr;    /* pointer to the current error */
  70.     int  qf_count;        /* number of errors (0 means no error list) */
  71.     int  qf_index;        /* current index in the error list */
  72.     int  qf_nonevalid;        /* TRUE if not a single valid entry found */
  73. } qf_lists[LISTCOUNT];
  74.  
  75. static int    qf_curlist = 0;    /* current error list */
  76. static int    qf_listcount = 0;   /* current number of lists */
  77.  
  78. #define FMT_PATTERNS 9        /* maximum number of % recognized */
  79.  
  80. /*
  81.  * Structure used to hold the info of one part of 'errorformat'
  82.  */
  83. struct eformat
  84. {
  85.     regprog_T        *prog;    /* pre-formatted part of 'errorformat' */
  86.     struct eformat  *next;    /* pointer to next (NULL if last) */
  87.     char_u        addr[FMT_PATTERNS]; /* indices of used % patterns */
  88.     char_u        prefix;    /* prefix of this format line: */
  89.                 /*   'D' enter directory */
  90.                 /*   'X' leave directory */
  91.                 /*   'A' start of multi-line message */
  92.                 /*   'E' error message */
  93.                 /*   'W' warning message */
  94.                 /*   'I' informational message */
  95.                 /*   'C' continuation line */
  96.                 /*   'Z' end of multi-line message */
  97.                 /*   'G' general, unspecific message */
  98.                 /*   'P' push file (partial) message */
  99.                 /*   'Q' pop/quit file (partial) message */
  100.                 /*   'O' overread (partial) message */
  101.     char_u        flags;    /* additional flags given in prefix */
  102.                 /*   '-' do not include this line */
  103. };
  104.  
  105. /*
  106.  * Read the errorfile into memory, line by line, building the error list.
  107.  * Return -1 for error, number of errors for success.
  108.  */
  109.     int
  110. qf_init(efile, errorformat, newlist)
  111.     char_u        *efile;
  112.     char_u        *errorformat;
  113.     int            newlist;        /* TRUE: start a new error list */
  114. {
  115.     char_u        *namebuf;
  116.     char_u        *errmsg;
  117.     char_u        *fmtstr = NULL;
  118.     int            col = 0;
  119.     char_u        use_virt_col = FALSE;
  120.     int            type = 0;
  121.     int            valid;
  122.     long        lnum = 0L;
  123.     int            enr = 0;
  124.     FILE        *fd;
  125.     struct qf_line  *qfp = NULL;
  126.     struct qf_line  *qfprev = NULL;    /* init to make SASC shut up */
  127.     char_u        *efmp;
  128.     struct eformat  *fmt_first = NULL;
  129.     struct eformat  *fmt_last = NULL;
  130.     struct eformat  *fmt_ptr;
  131.     char_u        *efm;
  132.     char_u        *ptr;
  133.     char_u        *srcptr;
  134.     int            len;
  135.     int            i;
  136.     int            round;
  137.     int            idx = 0;
  138.     int            multiline = FALSE;
  139.     int            multiignore = FALSE;
  140.     int            multiscan = FALSE;
  141.     int            retval = -1;    /* default: return error flag */
  142.     char_u        *directory = NULL;
  143.     char_u        *currfile = NULL;
  144.     char_u        *tail = NULL;
  145.     struct dir_stack_T  *file_stack = NULL;
  146.     regmatch_T        regmatch;
  147.     static struct fmtpattern
  148.     {
  149.     char_u    convchar;
  150.     char    *pattern;
  151.     }            fmt_pat[FMT_PATTERNS] =
  152.             {
  153.             {'f', "\\f\\+"},
  154.             {'n', "\\d\\+"},
  155.             {'l', "\\d\\+"},
  156.             {'c', "\\d\\+"},
  157.             {'t', "."},
  158.             {'m', ".\\+"},
  159.             {'r', ".*"},
  160.             {'p', "[- .]*"},
  161.             {'v', "\\d\\+"}
  162.             };
  163.  
  164.     if (efile == NULL)
  165.     return FAIL;
  166.  
  167.     namebuf = alloc(CMDBUFFSIZE + 1);
  168.     errmsg = alloc(CMDBUFFSIZE + 1);
  169.     if (namebuf == NULL || errmsg == NULL)
  170.     goto qf_init_end;
  171.  
  172.     if ((fd = mch_fopen((char *)efile, "r")) == NULL)
  173.     {
  174.     EMSG2(_(e_openerrf), efile);
  175.     goto qf_init_end;
  176.     }
  177.  
  178.     if (newlist || qf_curlist == qf_listcount)
  179.     {
  180.     /*
  181.      * If the current entry is not the last entry, delete entries below
  182.      * the current entry.  This makes it possible to browse in a tree-like
  183.      * way with ":grep'.
  184.      */
  185.     while (qf_listcount > qf_curlist + 1)
  186.         qf_free(--qf_listcount);
  187.  
  188.     /*
  189.      * When the stack is full, remove to oldest entry
  190.      * Otherwise, add a new entry.
  191.      */
  192.     if (qf_listcount == LISTCOUNT)
  193.     {
  194.         qf_free(0);
  195.         for (i = 1; i < LISTCOUNT; ++i)
  196.         qf_lists[i - 1] = qf_lists[i];
  197.         qf_curlist = LISTCOUNT - 1;
  198.     }
  199.     else
  200.         qf_curlist = qf_listcount++;
  201.     qf_lists[qf_curlist].qf_index = 0;
  202.     qf_lists[qf_curlist].qf_count = 0;
  203.     }
  204.     else if (qf_lists[qf_curlist].qf_count > 0)
  205.     {
  206.     /* Adding to existing list, find last entry. */
  207.     for (qfprev = qf_lists[qf_curlist].qf_start;
  208.                 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
  209.         ;
  210.     }
  211.  
  212. /*
  213.  * Each part of the format string is copied and modified from errorformat to
  214.  * regex prog.  Only a few % characters are allowed.
  215.  */
  216.     /* Use the local value of 'errorformat' if it's set. */
  217.     if (errorformat == p_efm && *curbuf->b_p_efm != NUL)
  218.     efm = curbuf->b_p_efm;
  219.     else
  220.     efm = errorformat;
  221.     /*
  222.      * Get some space to modify the format string into.
  223.      */
  224.     i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
  225.     for (round = FMT_PATTERNS; round > 0; )
  226.     i += (int)STRLEN(fmt_pat[--round].pattern);
  227. #ifdef COLON_IN_FILENAME
  228.     i += 12; /* "%f" can become twelve chars longer */
  229. #else
  230.     i += 2; /* "%f" can become two chars longer */
  231. #endif
  232.     if ((fmtstr = alloc(i)) == NULL)
  233.     goto error2;
  234.  
  235.     while (efm[0])
  236.     {
  237.     /*
  238.      * Allocate a new eformat structure and put it at the end of the list
  239.      */
  240.     fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
  241.     if (fmt_ptr == NULL)
  242.         goto error2;
  243.     if (fmt_first == NULL)        /* first one */
  244.         fmt_first = fmt_ptr;
  245.     else
  246.         fmt_last->next = fmt_ptr;
  247.     fmt_last = fmt_ptr;
  248.     fmt_ptr->prefix = NUL;
  249.     fmt_ptr->flags = NUL;
  250.     fmt_ptr->next = NULL;
  251.     fmt_ptr->prog = NULL;
  252.     for (round = FMT_PATTERNS; round > 0; )
  253.         fmt_ptr->addr[--round] = NUL;
  254.     /* round is 0 now */
  255.  
  256.     /*
  257.      * Isolate one part in the 'errorformat' option
  258.      */
  259.     for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
  260.         if (efm[len] == '\\' && efm[len + 1] != NUL)
  261.         ++len;
  262.  
  263.     /*
  264.      * Build regexp pattern from current 'errorformat' option
  265.      */
  266.     ptr = fmtstr;
  267.     *ptr++ = '^';
  268.     for (efmp = efm; efmp < efm + len; ++efmp)
  269.     {
  270.         if (*efmp == '%')
  271.         {
  272.         ++efmp;
  273.         for (idx = 0; idx < FMT_PATTERNS; ++idx)
  274.             if (fmt_pat[idx].convchar == *efmp)
  275.             break;
  276.         if (idx < FMT_PATTERNS)
  277.         {
  278.             if (fmt_ptr->addr[idx])
  279.             {
  280.             sprintf((char *)errmsg,
  281.                 _("E372: Too many %%%c in format string"), *efmp);
  282.             EMSG(errmsg);
  283.             goto error2;
  284.             }
  285.             if ((idx
  286.                 && idx < 6
  287.                 && vim_strchr((char_u *)"DXOPQ",
  288.                              fmt_ptr->prefix) != NULL)
  289.                 || (idx == 6
  290.                 && vim_strchr((char_u *)"OPQ",
  291.                             fmt_ptr->prefix) == NULL))
  292.             {
  293.             sprintf((char *)errmsg,
  294.                 _("E373: Unexpected %%%c in format string"), *efmp);
  295.             EMSG(errmsg);
  296.             goto error2;
  297.             }
  298.             fmt_ptr->addr[idx] = (char_u)++round;
  299.             *ptr++ = '\\';
  300.             *ptr++ = '(';
  301. #ifdef BACKSLASH_IN_FILENAME
  302.             if (*efmp == 'f')
  303.             {
  304.             /* Also match "c:" in the file name, even when
  305.              * checking for a colon next: "%f:".
  306.              * "\%(\a:\)\=" */
  307.             STRCPY(ptr, "\\%(\\a:\\)\\=");
  308.             ptr += 10;
  309.             }
  310. #endif
  311.             if (*efmp == 'f' && efmp[1] != NUL
  312.                      && efmp[1] != '\\' && efmp[1] != '%')
  313.             {
  314.             /* A file name may contain spaces, but this isn't in
  315.              * "\f".  use "[^x]\+" instead (x is next character) */
  316.             *ptr++ = '[';
  317.             *ptr++ = '^';
  318.             *ptr++ = efmp[1];
  319.             *ptr++ = ']';
  320.             *ptr++ = '\\';
  321.             *ptr++ = '+';
  322.             }
  323.             else
  324.             {
  325.             srcptr = (char_u *)fmt_pat[idx].pattern;
  326.             while ((*ptr = *srcptr++) != NUL)
  327.                 ++ptr;
  328.             }
  329.             *ptr++ = '\\';
  330.             *ptr++ = ')';
  331.         }
  332.         else if (*efmp == '*')
  333.         {
  334.             if (*++efmp == '[' || *efmp == '\\')
  335.             {
  336.             if ((*ptr++ = *efmp) == '[')    /* %*[^a-z0-9] etc. */
  337.             {
  338.                 if (efmp[1] == '^')
  339.                 *ptr++ = *++efmp;
  340.                 if (efmp < efm + len)
  341.                 {
  342.                 *ptr++ = *++efmp;        /* could be ']' */
  343.                 while (efmp < efm + len
  344.                     && (*ptr++ = *++efmp) != ']')
  345.                     /* skip */;
  346.                 if (efmp == efm + len)
  347.                 {
  348.                     EMSG(_("E374: Missing ] in format string"));
  349.                     goto error2;
  350.                 }
  351.                 }
  352.             }
  353.             else if (efmp < efm + len)    /* %*\D, %*\s etc. */
  354.                 *ptr++ = *++efmp;
  355.             *ptr++ = '\\';
  356.             *ptr++ = '+';
  357.             }
  358.             else
  359.             {
  360.             /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
  361.             sprintf((char *)errmsg,
  362.                 _("E375: Unsupported %%%c in format string"), *efmp);
  363.             EMSG(errmsg);
  364.             goto error2;
  365.             }
  366.         }
  367.         else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
  368.             *ptr++ = *efmp;        /* regexp magic characters */
  369.         else if (*efmp == '#')
  370.             *ptr++ = '*';
  371.         else if (efmp == efm + 1)        /* analyse prefix */
  372.         {
  373.             if (vim_strchr((char_u *)"+-", *efmp) != NULL)
  374.             fmt_ptr->flags = *efmp++;
  375.             if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
  376.             fmt_ptr->prefix = *efmp;
  377.             else
  378.             {
  379.             sprintf((char *)errmsg,
  380.                 _("E376: Invalid %%%c in format string prefix"), *efmp);
  381.             EMSG(errmsg);
  382.             goto error2;
  383.             }
  384.         }
  385.         else
  386.         {
  387.             sprintf((char *)errmsg,
  388.                 _("E377: Invalid %%%c in format string"), *efmp);
  389.             EMSG(errmsg);
  390.             goto error2;
  391.         }
  392.         }
  393.         else            /* copy normal character */
  394.         {
  395.         if (*efmp == '\\' && efmp + 1 < efm + len)
  396.             ++efmp;
  397.         else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
  398.             *ptr++ = '\\';    /* escape regexp atoms */
  399.         if (*efmp)
  400.             *ptr++ = *efmp;
  401.         }
  402.     }
  403.     *ptr++ = '$';
  404.     *ptr = NUL;
  405.     if ((fmt_ptr->prog = vim_regcomp(fmtstr, TRUE)) == NULL)
  406.         goto error2;
  407.     /*
  408.      * Advance to next part
  409.      */
  410.     efm = skip_to_option_part(efm + len);    /* skip comma and spaces */
  411.     }
  412.     if (fmt_first == NULL)    /* nothing found */
  413.     {
  414.     EMSG(_("E378: 'errorformat' contains no pattern"));
  415.     goto error2;
  416.     }
  417.  
  418.     /*
  419.      * got_int is reset here, because it was probably set when killing the
  420.      * ":make" command, but we still want to read the errorfile then.
  421.      */
  422.     got_int = FALSE;
  423.  
  424.     /* Always ignore case when looking for a matching error. */
  425.     regmatch.rm_ic = TRUE;
  426.  
  427.     /*
  428.      * Read the lines in the error file one by one.
  429.      * Try to recognize one of the error formats in each line.
  430.      */
  431.     while (fgets((char *)IObuff, CMDBUFFSIZE, fd) != NULL && !got_int)
  432.     {
  433.     IObuff[CMDBUFFSIZE] = NUL;  /* for very long lines */
  434.     if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
  435.         *efmp = NUL;
  436. #ifdef USE_CRNL
  437.     if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
  438.         *efmp = NUL;
  439. #endif
  440.  
  441.     /*
  442.      * Try to match each part of 'errorformat' until we find a complete
  443.      * match or no match.
  444.      */
  445.     valid = TRUE;
  446. restofline:
  447.     for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
  448.     {
  449.         idx = fmt_ptr->prefix;
  450.         if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
  451.         continue;
  452.         namebuf[0] = NUL;
  453.         if (!multiscan)
  454.         errmsg[0] = NUL;
  455.         lnum = 0;
  456.         col = 0;
  457.         use_virt_col = FALSE;
  458.         enr = -1;
  459.         type = 0;
  460.         tail = NULL;
  461.  
  462.         regmatch.regprog = fmt_ptr->prog;
  463.         if (vim_regexec(®match, IObuff, (colnr_T)0))
  464.         {
  465.         if ((idx == 'C' || idx == 'Z') && !multiline)
  466.             continue;
  467.         if (vim_strchr((char_u *)"EWI", idx) != NULL)
  468.             type = idx;
  469.         else
  470.             type = 0;
  471.         /*
  472.          * Extract error message data from matched line
  473.          */
  474.         if ((i = (int)fmt_ptr->addr[0]) > 0)        /* %f */
  475.         {
  476.             len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  477.             STRNCPY(namebuf, regmatch.startp[i], len);
  478.             namebuf[len] = NUL;
  479.             if (vim_strchr((char_u *)"OPQ", idx) != NULL
  480.                 && mch_getperm(namebuf) == -1)
  481.             continue;
  482.         }
  483.         if ((i = (int)fmt_ptr->addr[1]) > 0)        /* %n */
  484.             enr = (int)atol((char *)regmatch.startp[i]);
  485.         if ((i = (int)fmt_ptr->addr[2]) > 0)        /* %l */
  486.             lnum = atol((char *)regmatch.startp[i]);
  487.         if ((i = (int)fmt_ptr->addr[3]) > 0)        /* %c */
  488.             col = (int)atol((char *)regmatch.startp[i]);
  489.         if ((i = (int)fmt_ptr->addr[4]) > 0)        /* %t */
  490.             type = *regmatch.startp[i];
  491.         if (fmt_ptr->flags ==  '+' && !multiscan)    /* %+ */
  492.             STRCPY(errmsg, IObuff);
  493.         else if ((i = (int)fmt_ptr->addr[5]) > 0)    /* %m */
  494.         {
  495.             len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  496.             STRNCPY(errmsg, regmatch.startp[i], len);
  497.             errmsg[len] = NUL;
  498.         }
  499.         if ((i = (int)fmt_ptr->addr[6]) > 0)        /* %r */
  500.             tail = regmatch.startp[i];
  501.         if ((i = (int)fmt_ptr->addr[7]) > 0)        /* %p */
  502.             col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
  503.         if ((i = (int)fmt_ptr->addr[8]) > 0)        /* %v */
  504.         {
  505.             col = (int)atol((char *)regmatch.startp[i]);
  506.             use_virt_col = TRUE;
  507.         }
  508.         break;
  509.         }
  510.     }
  511.     multiscan = FALSE;
  512.     if (!fmt_ptr || idx == 'D' || idx == 'X')
  513.     {
  514.         if (fmt_ptr)
  515.         {
  516.         if (idx == 'D')                /* enter directory */
  517.         {
  518.             if (*namebuf == NUL)
  519.             {
  520.             EMSG(_("E379: Missing or empty directory name"));
  521.             goto error1;
  522.             }
  523.             if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
  524.             goto error1;
  525.         }
  526.         else if (idx == 'X')            /* leave directory */
  527.             directory = qf_pop_dir(&dir_stack);
  528.         }
  529.         namebuf[0] = NUL;        /* no match found, remove file name */
  530.         lnum = 0;            /* don't jump to this line */
  531.         valid = FALSE;
  532.         STRCPY(errmsg, IObuff);    /* copy whole line to error message */
  533.         if (!fmt_ptr)
  534.         multiline = multiignore = FALSE;
  535.     }
  536.     else if (fmt_ptr)
  537.     {
  538.         if (vim_strchr((char_u *)"AEWI", idx) != NULL)
  539.         multiline = TRUE;    /* start of a multi-line message */
  540.         else if (vim_strchr((char_u *)"CZ", idx) != NULL)
  541.         {                /* continuation of multi-line msg */
  542.         if (qfprev == NULL)
  543.             goto error1;
  544.         if (*errmsg && !multiignore)
  545.         {
  546.             len = (int)STRLEN(qfprev->qf_text);
  547.             if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
  548.                                     == NULL)
  549.             goto error1;
  550.             STRCPY(ptr, qfprev->qf_text);
  551.             vim_free(qfprev->qf_text);
  552.             qfprev->qf_text = ptr;
  553.             *(ptr += len) = '\n';
  554.             STRCPY(++ptr, errmsg);
  555.         }
  556.         if (qfp->qf_nr == -1)
  557.             qfp->qf_nr = enr;
  558.         if (vim_isprintc(type) && !qfp->qf_type)
  559.             qfp->qf_type = type;    /* only printable chars allowed */
  560.         if (!qfp->qf_lnum)
  561.             qfp->qf_lnum = lnum;
  562.         if (!qfp->qf_col)
  563.             qfp->qf_col = col;
  564.         qfp->qf_virt_col = use_virt_col;
  565.         if (!qfp->qf_fnum)
  566.             qfp->qf_fnum = qf_get_fnum(directory,
  567.                     *namebuf || directory ? namebuf
  568.                       : currfile && valid ? currfile : 0);
  569.         if (idx == 'Z')
  570.             multiline = multiignore = FALSE;
  571.         line_breakcheck();
  572.         continue;
  573.         }
  574.         else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
  575.         {
  576.         /* global file names */
  577.         valid = FALSE;
  578.         if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
  579.         {
  580.             if (*namebuf && idx == 'P')
  581.             currfile = qf_push_dir(namebuf, &file_stack);
  582.             else if (idx == 'Q')
  583.             currfile = qf_pop_dir(&file_stack);
  584.             *namebuf = NUL;
  585.             if (tail && *tail)
  586.             {
  587.             STRCPY(IObuff, skipwhite(tail));
  588.             multiscan = TRUE;
  589.             goto restofline;
  590.             }
  591.         }
  592.         }
  593.         if (fmt_ptr->flags == '-')    /* generally exclude this line */
  594.         {
  595.         if (multiline)
  596.             multiignore = TRUE;    /* also exclude continuation lines */
  597.         continue;
  598.         }
  599.     }
  600.  
  601.     if ((qfp = (struct qf_line *)alloc((unsigned)sizeof(struct qf_line)))
  602.                                       == NULL)
  603.         goto error2;
  604.  
  605.     qfp->qf_fnum = qf_get_fnum(directory, *namebuf || directory ?
  606.                   namebuf : currfile && valid ? currfile : 0);
  607.     if ((qfp->qf_text = vim_strsave(errmsg)) == NULL)
  608.         goto error1;
  609.     if (!vim_isprintc(type))    /* only printable chars allowed */
  610.         type = 0;
  611.     qfp->qf_lnum = lnum;
  612.     qfp->qf_col = col;
  613.     qfp->qf_virt_col = use_virt_col;
  614.     qfp->qf_nr = enr;
  615.     qfp->qf_type = type;
  616.     qfp->qf_valid = valid;
  617.  
  618.     if (qf_lists[qf_curlist].qf_count == 0)    /* first element in the list */
  619.     {
  620.         qf_lists[qf_curlist].qf_start = qfp;
  621.         qfp->qf_prev = qfp;    /* first element points to itself */
  622.     }
  623.     else
  624.     {
  625.         qfp->qf_prev = qfprev;
  626.         qfprev->qf_next = qfp;
  627.     }
  628.     qfp->qf_next = qfp;    /* last element points to itself */
  629.     qfp->qf_cleared = FALSE;
  630.     qfprev = qfp;
  631.     ++qf_lists[qf_curlist].qf_count;
  632.     if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
  633.                         /* first valid entry */
  634.     {
  635.         qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
  636.         qf_lists[qf_curlist].qf_ptr = qfp;
  637.     }
  638.     line_breakcheck();
  639.     }
  640.     if (!ferror(fd))
  641.     {
  642.     if (qf_lists[qf_curlist].qf_index == 0)    /* no valid entry found */
  643.     {
  644.         qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
  645.         qf_lists[qf_curlist].qf_index = 1;
  646.         qf_lists[qf_curlist].qf_nonevalid = TRUE;
  647.     }
  648.     else
  649.     {
  650.         qf_lists[qf_curlist].qf_nonevalid = FALSE;
  651.         if (qf_lists[qf_curlist].qf_ptr == NULL)
  652.         qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
  653.     }
  654.     retval = qf_lists[qf_curlist].qf_count;    /* return number of matches */
  655.     goto qf_init_ok;
  656.     }
  657.     EMSG(_(e_readerrf));
  658. error1:
  659.     vim_free(qfp);
  660. error2:
  661.     qf_free(qf_curlist);
  662.     qf_listcount--;
  663.     if (qf_curlist > 0)
  664.     --qf_curlist;
  665. qf_init_ok:
  666.     fclose(fd);
  667.     for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
  668.     {
  669.     fmt_first = fmt_ptr->next;
  670.     vim_free(fmt_ptr->prog);
  671.     vim_free(fmt_ptr);
  672.     }
  673.     qf_clean_dir_stack(&dir_stack);
  674.     qf_clean_dir_stack(&file_stack);
  675. qf_init_end:
  676.     vim_free(namebuf);
  677.     vim_free(errmsg);
  678.     vim_free(fmtstr);
  679.  
  680. #ifdef FEAT_WINDOWS
  681.     qf_update_buffer();
  682. #endif
  683.  
  684.     return retval;
  685. }
  686.  
  687. /*
  688.  * get buffer number for file "dir.name"
  689.  */
  690.     static int
  691. qf_get_fnum(directory, fname)
  692.     char_u   *directory;
  693.     char_u   *fname;
  694. {
  695.     if (fname == NULL || *fname == NUL)        /* no file name */
  696.     return 0;
  697.     {
  698. #ifdef RISCOS
  699.     /* Name is reported as `main.c', but file is `c.main' */
  700.     return ro_buflist_add(fname);
  701. #else
  702.     char_u        *ptr;
  703.     int        fnum;
  704.  
  705.     if (directory != NULL && !vim_isAbsName(fname)
  706.         && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
  707.     {
  708.         /*
  709.          * Here we check if the file really exists.
  710.          * This should normally be true, but if make works without
  711.          * "leaving directory"-messages we might have missed a
  712.          * directory change.
  713.          */
  714.         if (mch_getperm(ptr) < 0)
  715.         {
  716.         vim_free(ptr);
  717.         directory = qf_guess_filepath(fname);
  718.         if (directory)
  719.             ptr = concat_fnames(directory, fname, TRUE);
  720.         else
  721.             ptr = vim_strsave(fname);
  722.         }
  723.         /* Use concatenated directory name and file name */
  724.         fnum = buflist_add(ptr, 0);
  725.         vim_free(ptr);
  726.         return fnum;
  727.     }
  728.     return buflist_add(fname, 0);
  729. #endif
  730.     }
  731. }
  732.  
  733. /*
  734.  * push dirbuf onto the directory stack and return pointer to actual dir or
  735.  * NULL on error
  736.  */
  737.     static char_u *
  738. qf_push_dir(dirbuf, stackptr)
  739.     char_u        *dirbuf;
  740.     struct dir_stack_T    **stackptr;
  741. {
  742.     struct dir_stack_T  *ds_new;
  743.     struct dir_stack_T  *ds_ptr;
  744.  
  745.     /* allocate new stack element and hook it in */
  746.     ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
  747.     if (ds_new == NULL)
  748.     return NULL;
  749.  
  750.     ds_new->next = *stackptr;
  751.     *stackptr = ds_new;
  752.  
  753.     /* store directory on the stack */
  754.     if (vim_isAbsName(dirbuf)
  755.         || (*stackptr)->next == NULL
  756.         || (*stackptr && dir_stack != *stackptr))
  757.     (*stackptr)->dirname = vim_strsave(dirbuf);
  758.     else
  759.     {
  760.     /* Okay we don't have an absolute path.
  761.      * dirbuf must be a subdir of one of the directories on the stack.
  762.      * Let's search...
  763.      */
  764.     ds_new = (*stackptr)->next;
  765.     (*stackptr)->dirname = NULL;
  766.     while (ds_new)
  767.     {
  768.         vim_free((*stackptr)->dirname);
  769.         (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
  770.             TRUE);
  771.         if (mch_isdir((*stackptr)->dirname) == TRUE)
  772.         break;
  773.  
  774.         ds_new = ds_new->next;
  775.     }
  776.  
  777.     /* clean up all dirs we already left */
  778.     while ((*stackptr)->next != ds_new)
  779.     {
  780.         ds_ptr = (*stackptr)->next;
  781.         (*stackptr)->next = (*stackptr)->next->next;
  782.         vim_free(ds_ptr->dirname);
  783.         vim_free(ds_ptr);
  784.     }
  785.  
  786.     /* Nothing found -> it must be on top level */
  787.     if (ds_new == NULL)
  788.     {
  789.         vim_free((*stackptr)->dirname);
  790.         (*stackptr)->dirname = vim_strsave(dirbuf);
  791.     }
  792.     }
  793.  
  794.     if ((*stackptr)->dirname != NULL)
  795.     return (*stackptr)->dirname;
  796.     else
  797.     {
  798.     ds_ptr = *stackptr;
  799.     *stackptr = (*stackptr)->next;
  800.     vim_free(ds_ptr);
  801.     return NULL;
  802.     }
  803. }
  804.  
  805.  
  806. /*
  807.  * pop dirbuf from the directory stack and return previous directory or NULL if
  808.  * stack is empty
  809.  */
  810.     static char_u *
  811. qf_pop_dir(stackptr)
  812.     struct dir_stack_T    **stackptr;
  813. {
  814.     struct dir_stack_T  *ds_ptr;
  815.  
  816.     /* TODO: Should we check if dirbuf is the directory on top of the stack?
  817.      * What to do if it isn't? */
  818.  
  819.     /* pop top element and free it */
  820.     if (*stackptr != NULL)
  821.     {
  822.     ds_ptr = *stackptr;
  823.     *stackptr = (*stackptr)->next;
  824.     vim_free(ds_ptr->dirname);
  825.     vim_free(ds_ptr);
  826.     }
  827.  
  828.     /* return NEW top element as current dir or NULL if stack is empty*/
  829.     return *stackptr ? (*stackptr)->dirname : NULL;
  830. }
  831.  
  832. /*
  833.  * clean up directory stack
  834.  */
  835.     static void
  836. qf_clean_dir_stack(stackptr)
  837.     struct dir_stack_T    **stackptr;
  838. {
  839.     struct dir_stack_T  *ds_ptr;
  840.  
  841.     while ((ds_ptr = *stackptr) != NULL)
  842.     {
  843.     *stackptr = (*stackptr)->next;
  844.     vim_free(ds_ptr->dirname);
  845.     vim_free(ds_ptr);
  846.     }
  847. }
  848.  
  849. /*
  850.  * Check in which directory of the directory stack the given file can be
  851.  * found.
  852.  * Returns a pointer to the directory name or NULL if not found
  853.  * Cleans up intermediate directory entries.
  854.  *
  855.  * TODO: How to solve the following problem?
  856.  * If we have the this directory tree:
  857.  *     ./
  858.  *     ./aa
  859.  *     ./aa/bb
  860.  *     ./bb
  861.  *     ./bb/x.c
  862.  * and make says:
  863.  *     making all in aa
  864.  *     making all in bb
  865.  *     x.c:9: Error
  866.  * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
  867.  * qf_guess_filepath will return NULL.
  868.  */
  869.     static char_u *
  870. qf_guess_filepath(filename)
  871.     char_u *filename;
  872. {
  873.     struct dir_stack_T     *ds_ptr;
  874.     struct dir_stack_T     *ds_tmp;
  875.     char_u           *fullname;
  876.  
  877.     /* no dirs on the stack - there's nothing we can do */
  878.     if (dir_stack == NULL)
  879.     return NULL;
  880.  
  881.     ds_ptr = dir_stack->next;
  882.     fullname = NULL;
  883.     while (ds_ptr)
  884.     {
  885.     vim_free(fullname);
  886.     fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
  887.  
  888.     /* If concat_fnames failed, just go on. The worst thing that can happen
  889.      * is that we delete the entire stack.
  890.      */
  891.     if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
  892.         break;
  893.  
  894.     ds_ptr = ds_ptr->next;
  895.     }
  896.  
  897.     vim_free(fullname);
  898.  
  899.     /* clean up all dirs we already left */
  900.     while (dir_stack->next != ds_ptr)
  901.     {
  902.     ds_tmp = dir_stack->next;
  903.     dir_stack->next = dir_stack->next->next;
  904.     vim_free(ds_tmp->dirname);
  905.     vim_free(ds_tmp);
  906.     }
  907.  
  908.     return ds_ptr==NULL? NULL: ds_ptr->dirname;
  909.  
  910. }
  911.  
  912. /*
  913.  * jump to a quickfix line
  914.  * if dir == FORWARD go "errornr" valid entries forward
  915.  * if dir == BACKWARD go "errornr" valid entries backward
  916.  * else if "errornr" is zero, redisplay the same line
  917.  * else go to entry "errornr"
  918.  */
  919.     void
  920. qf_jump(dir, errornr, forceit)
  921.     int        dir;
  922.     int        errornr;
  923.     int        forceit;
  924. {
  925.     struct qf_line    *qf_ptr;
  926.     struct qf_line    *old_qf_ptr;
  927.     int            qf_index;
  928.     int            old_qf_fnum;
  929.     int            old_qf_index;
  930.     int            prev_index;
  931.     static char_u    *e_no_more_items = (char_u *)N_("No more items");
  932.     char_u        *err = e_no_more_items;
  933.     linenr_T        i;
  934.     buf_T        *old_curbuf;
  935.     linenr_T        old_lnum;
  936.     char_u        *old_swb = p_swb;
  937.     colnr_T        screen_col;
  938.     colnr_T        char_col;
  939.     char_u        *line;
  940. #ifdef FEAT_WINDOWS
  941.     int            opened_window = FALSE;
  942.     win_T        *win;
  943. #endif
  944.     int            print_message = TRUE;
  945.     int            len;
  946. #ifdef FEAT_FOLDING
  947.     int            old_KeyTyped = KeyTyped; /* getting file may reset it */
  948. #endif
  949.  
  950.     if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
  951.     {
  952.     EMSG(_(e_quickfix));
  953.     return;
  954.     }
  955.  
  956.     qf_ptr = qf_lists[qf_curlist].qf_ptr;
  957.     old_qf_ptr = qf_ptr;
  958.     qf_index = qf_lists[qf_curlist].qf_index;
  959.     old_qf_index = qf_index;
  960.     if (dir == FORWARD || dir == FORWARD_FILE)        /* next valid entry */
  961.     {
  962.     while (errornr--)
  963.     {
  964.         old_qf_ptr = qf_ptr;
  965.         prev_index = qf_index;
  966.         old_qf_fnum = qf_ptr->qf_fnum;
  967.         do
  968.         {
  969.         if (qf_index == qf_lists[qf_curlist].qf_count
  970.                            || qf_ptr->qf_next == NULL)
  971.         {
  972.             qf_ptr = old_qf_ptr;
  973.             qf_index = prev_index;
  974.             if (err != NULL)
  975.             {
  976.             EMSG(_(err));
  977.             goto theend;
  978.             }
  979.             errornr = 0;
  980.             break;
  981.         }
  982.         ++qf_index;
  983.         qf_ptr = qf_ptr->qf_next;
  984.         } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
  985.           || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  986.         err = NULL;
  987.     }
  988.     }
  989.     else if (dir == BACKWARD)        /* previous valid entry */
  990.     {
  991.     while (errornr--)
  992.     {
  993.         old_qf_ptr = qf_ptr;
  994.         prev_index = qf_index;
  995.         do
  996.         {
  997.         if (qf_index == 1 || qf_ptr->qf_prev == NULL)
  998.         {
  999.             qf_ptr = old_qf_ptr;
  1000.             qf_index = prev_index;
  1001.             if (err != NULL)
  1002.             {
  1003.             EMSG(_(err));
  1004.             goto theend;
  1005.             }
  1006.             errornr = 0;
  1007.             break;
  1008.         }
  1009.         --qf_index;
  1010.         qf_ptr = qf_ptr->qf_prev;
  1011.         } while (!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid);
  1012.         err = NULL;
  1013.     }
  1014.     }
  1015.     else if (errornr != 0)    /* go to specified number */
  1016.     {
  1017.     while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
  1018.     {
  1019.         --qf_index;
  1020.         qf_ptr = qf_ptr->qf_prev;
  1021.     }
  1022.     while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
  1023.                            && qf_ptr->qf_next != NULL)
  1024.     {
  1025.         ++qf_index;
  1026.         qf_ptr = qf_ptr->qf_next;
  1027.     }
  1028.     }
  1029.  
  1030. #ifdef FEAT_WINDOWS
  1031.     qf_lists[qf_curlist].qf_index = qf_index;
  1032.     if (qf_win_pos_update(old_qf_index))
  1033.     /* No need to print the error message if it's visible in the error
  1034.      * window */
  1035.     print_message = FALSE;
  1036.  
  1037.     /*
  1038.      * If currently in the quickfix window, find another window to show the
  1039.      * file in.
  1040.      */
  1041.     if (bt_quickfix(curbuf))
  1042.     {
  1043.     /*
  1044.      * If there is no file specified, we don't know where to go.
  1045.      * But do advance, otherwise ":cn" gets stuck.
  1046.      */
  1047.     if (qf_ptr->qf_fnum == 0)
  1048.         goto theend;
  1049.  
  1050.     /*
  1051.      * If there is only one window, create a new one above the quickfix
  1052.      * window.
  1053.      */
  1054.     if (firstwin == lastwin)
  1055.     {
  1056.         if (win_split(0, WSP_ABOVE) == FAIL)
  1057.         goto failed;        /* not enough room for window */
  1058.         opened_window = TRUE;    /* close it when fail */
  1059.         p_swb = empty_option;    /* don't split again */
  1060.     }
  1061.     else
  1062.     {
  1063.         /*
  1064.          * Try to find a window that shows the right buffer.
  1065.          * Default to the window just above the quickfix buffer.
  1066.          */
  1067.         win = curwin->w_prev;
  1068.         for (;;)
  1069.         {
  1070.         if (win == NULL || bt_quickfix(win->w_buffer))
  1071.         {
  1072.             /* Didn't find it, go to the window above the quickfix
  1073.              * window. */
  1074.             if (curwin->w_prev == NULL)
  1075.             win = curwin->w_next;
  1076.             else
  1077.             win = curwin->w_prev;
  1078.             break;
  1079.         }
  1080.         if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1081.             break;
  1082.         if (win->w_prev == NULL)
  1083.             win = lastwin;    /* wrap around the top */
  1084.         else
  1085.             win = win->w_prev;    /* go to previous window */
  1086.         }
  1087.         win_goto(win);
  1088.     }
  1089.     }
  1090. #endif
  1091.  
  1092.     /*
  1093.      * If there is a file name,
  1094.      * read the wanted file if needed, and check autowrite etc.
  1095.      */
  1096.     old_curbuf = curbuf;
  1097.     old_lnum = curwin->w_cursor.lnum;
  1098.     if (qf_ptr->qf_fnum == 0 || buflist_getfile(qf_ptr->qf_fnum,
  1099.             (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit) == OK)
  1100.     {
  1101.     /* When not switched to another buffer, still need to set pc mark */
  1102.     if (curbuf == old_curbuf)
  1103.         setpcmark();
  1104.  
  1105.     /*
  1106.      * Go to line with error, unless qf_lnum is 0.
  1107.      */
  1108.     i = qf_ptr->qf_lnum;
  1109.     if (i > 0)
  1110.     {
  1111.         if (i > curbuf->b_ml.ml_line_count)
  1112.         i = curbuf->b_ml.ml_line_count;
  1113.         curwin->w_cursor.lnum = i;
  1114.     }
  1115.     if (qf_ptr->qf_col > 0)
  1116.     {
  1117.         curwin->w_cursor.col = qf_ptr->qf_col - 1;
  1118.         if (qf_ptr->qf_virt_col == TRUE)
  1119.         {
  1120.         /*
  1121.          * Check each character from the beginning of the error
  1122.          * line up to the error column.  For each tab character
  1123.          * found, reduce the error column value by the length of
  1124.          * a tab character.
  1125.          */
  1126.         line = ml_get_curline();
  1127.         screen_col = 0;
  1128.         for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
  1129.         {
  1130.             if (*line++ == '\t')
  1131.             {
  1132.             curwin->w_cursor.col -= 7 - (screen_col % 8);
  1133.             screen_col += 8 - (screen_col % 8);
  1134.             }
  1135.             else
  1136.             ++screen_col;
  1137.         }
  1138.         }
  1139.         check_cursor();
  1140.     }
  1141.     else
  1142.         beginline(BL_WHITE | BL_FIX);
  1143.  
  1144. #ifdef FEAT_FOLDING
  1145.     if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
  1146.         foldOpenCursor();
  1147. #endif
  1148.     if (print_message)
  1149.     {
  1150.         /* Update the screen before showing the message */
  1151.         update_topline_redraw();
  1152.         sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
  1153.             qf_lists[qf_curlist].qf_count,
  1154.             qf_ptr->qf_cleared ? (char_u *)_(" (line deleted)")
  1155.                                    : (char_u *)"",
  1156.             qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
  1157.         /* Add the message, skipping leading whitespace and newlines. */
  1158.         len = (int)STRLEN(IObuff);
  1159.         qf_fmt_text(qf_ptr->qf_text, IObuff + len, IOSIZE - len);
  1160.  
  1161.         /* Output the message.  Overwrite to avoid scrolling when the 'O'
  1162.          * flag is present in 'shortmess'; But when not jumping, print the
  1163.          * whole message. */
  1164.         i = msg_scroll;
  1165.         if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
  1166.         msg_scroll = TRUE;
  1167.         else if (!msg_scrolled && shortmess(SHM_OVERALL))
  1168.         msg_scroll = FALSE;
  1169.         msg_attr_keep(IObuff, 0, TRUE);
  1170.         msg_scroll = i;
  1171.     }
  1172.     }
  1173.     else
  1174.     {
  1175. #ifdef FEAT_WINDOWS
  1176.     if (opened_window)
  1177.         win_close(curwin, TRUE);    /* Close opened window */
  1178. #endif
  1179.     if (qf_ptr->qf_fnum != 0)
  1180.     {
  1181.         /*
  1182.          * Couldn't open file, so put index back where it was.  This could
  1183.          * happen if the file was readonly and we changed something.
  1184.          */
  1185. #ifdef FEAT_WINDOWS
  1186. failed:
  1187. #endif
  1188.         qf_ptr = old_qf_ptr;
  1189.         qf_index = old_qf_index;
  1190.     }
  1191.     }
  1192. theend:
  1193.     qf_lists[qf_curlist].qf_ptr = qf_ptr;
  1194.     qf_lists[qf_curlist].qf_index = qf_index;
  1195. #ifdef FEAT_WINDOWS
  1196.     if (p_swb != old_swb && opened_window)
  1197.     {
  1198.     /* Restore old 'switchbuf' value, but not when an autocommand or
  1199.      * modeline has changed the value. */
  1200.     if (p_swb == empty_option)
  1201.         p_swb = old_swb;
  1202.     else
  1203.         free_string_option(old_swb);
  1204.     }
  1205. #endif
  1206. }
  1207.  
  1208. /*
  1209.  * ":clist": list all errors
  1210.  */
  1211.     void
  1212. qf_list(eap)
  1213.     exarg_T    *eap;
  1214. {
  1215.     buf_T        *buf;
  1216.     char_u        *fname;
  1217.     struct qf_line    *qfp;
  1218.     int            i;
  1219.     int            idx1 = 1;
  1220.     int            idx2 = -1;
  1221.     int            need_return = TRUE;
  1222.     int            last_printed = 1;
  1223.     char_u        *arg = eap->arg;
  1224.     int            all = eap->forceit;    /* if not :cl!, only show
  1225.                            recognised errors */
  1226.  
  1227.     if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
  1228.     {
  1229.     EMSG(_(e_quickfix));
  1230.     return;
  1231.     }
  1232.     if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
  1233.     {
  1234.     EMSG(_(e_trailing));
  1235.     return;
  1236.     }
  1237.     i = qf_lists[qf_curlist].qf_count;
  1238.     if (idx1 < 0)
  1239.     idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
  1240.     if (idx2 < 0)
  1241.     idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
  1242.  
  1243.     more_back_used = TRUE;
  1244.     if (qf_lists[qf_curlist].qf_nonevalid)
  1245.     all = TRUE;
  1246.     qfp = qf_lists[qf_curlist].qf_start;
  1247.     for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
  1248.     {
  1249.     if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
  1250.     {
  1251.         if (need_return)
  1252.         {
  1253.         msg_putchar('\n');
  1254.         need_return = FALSE;
  1255.         }
  1256.         if (more_back == 0)
  1257.         {
  1258.         fname = NULL;
  1259.         if (qfp->qf_fnum != 0
  1260.                   && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
  1261.             fname = buf->b_fname;
  1262.         if (fname == NULL)
  1263.             sprintf((char *)IObuff, "%2d", i);
  1264.         else
  1265.             sprintf((char *)IObuff, "%2d %s", i, fname);
  1266.         msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
  1267.             ? hl_attr(HLF_L) : hl_attr(HLF_D));
  1268.         if (qfp->qf_lnum == 0)
  1269.             IObuff[0] = NUL;
  1270.         else if (qfp->qf_col == 0)
  1271.             sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
  1272.         else
  1273.             sprintf((char *)IObuff, ":%ld col %d",
  1274.                            qfp->qf_lnum, qfp->qf_col);
  1275.         sprintf((char *)IObuff + STRLEN(IObuff), "%s: ",
  1276.                       qf_types(qfp->qf_type, qfp->qf_nr));
  1277.         msg_puts_attr(IObuff, hl_attr(HLF_N));
  1278.         /* Remove newlines and leading whitespace from the text. */
  1279.         qf_fmt_text(qfp->qf_text, IObuff, IOSIZE);
  1280.         msg_prt_line(IObuff);
  1281.         out_flush();        /* show one line at a time */
  1282.         need_return = TRUE;
  1283.         last_printed = i;
  1284.         }
  1285.     }
  1286.     if (more_back)
  1287.     {
  1288.         /* scrolling backwards from the more-prompt */
  1289.         /* TODO: compute the number of items from the screen lines */
  1290.         more_back = more_back * 2 - 1;
  1291.         while (i > last_printed - more_back && i > idx1)
  1292.         {
  1293.         do
  1294.         {
  1295.             qfp = qfp->qf_prev;
  1296.             --i;
  1297.         }
  1298.         while (i > idx1 && !qfp->qf_valid && !all);
  1299.         }
  1300.         more_back = 0;
  1301.     }
  1302.     else
  1303.     {
  1304.         qfp = qfp->qf_next;
  1305.         ++i;
  1306.     }
  1307.     ui_breakcheck();
  1308.     }
  1309.     more_back_used = FALSE;
  1310. }
  1311.  
  1312. /*
  1313.  * Remove newlines and leading whitespace from an error message.
  1314.  * Put the result in "buf[bufsize]".
  1315.  */
  1316.     static void
  1317. qf_fmt_text(text, buf, bufsize)
  1318.     char_u    *text;
  1319.     char_u    *buf;
  1320.     int        bufsize;
  1321. {
  1322.     int        i;
  1323.     char_u    *p = skipwhite(text);
  1324.  
  1325.     for (i = 0; *p != NUL && i < bufsize - 1; ++i)
  1326.     {
  1327.     if (*p == '\n')
  1328.     {
  1329.         buf[i] = ' ';
  1330.         while (*++p != NUL)
  1331.         if (!vim_iswhite(*p) && *p != '\n')
  1332.             break;
  1333.     }
  1334.     else
  1335.         buf[i] = *p++;
  1336.     }
  1337.     buf[i] = NUL;
  1338. }
  1339.  
  1340. /*
  1341.  * ":colder [count]": Up in the quickfix stack.
  1342.  * ":cnewer [count]": Down in the quickfix stack.
  1343.  */
  1344.     void
  1345. qf_age(eap)
  1346.     exarg_T    *eap;
  1347. {
  1348.     int        count;
  1349.  
  1350.     if (eap->addr_count != 0)
  1351.     count = eap->line2;
  1352.     else
  1353.     count = 1;
  1354.     while (count--)
  1355.     {
  1356.     if (eap->cmdidx == CMD_colder)
  1357.     {
  1358.         if (qf_curlist == 0)
  1359.         {
  1360.         EMSG(_("E380: At bottom of quickfix stack"));
  1361.         return;
  1362.         }
  1363.         --qf_curlist;
  1364.     }
  1365.     else
  1366.     {
  1367.         if (qf_curlist >= qf_listcount - 1)
  1368.         {
  1369.         EMSG(_("E381: At top of quickfix stack"));
  1370.         return;
  1371.         }
  1372.         ++qf_curlist;
  1373.     }
  1374.     }
  1375.     qf_msg();
  1376. }
  1377.  
  1378.     static void
  1379. qf_msg()
  1380. {
  1381.     smsg((char_u *)_("error list %d of %d; %d errors"),
  1382.         qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
  1383. #ifdef FEAT_WINDOWS
  1384.     qf_update_buffer();
  1385. #endif
  1386. }
  1387.  
  1388. /*
  1389.  * free the error list
  1390.  */
  1391.     static void
  1392. qf_free(idx)
  1393.     int        idx;
  1394. {
  1395.     struct qf_line *qfp;
  1396.  
  1397.     while (qf_lists[idx].qf_count)
  1398.     {
  1399.     qfp = qf_lists[idx].qf_start->qf_next;
  1400.     vim_free(qf_lists[idx].qf_start->qf_text);
  1401.     vim_free(qf_lists[idx].qf_start);
  1402.     qf_lists[idx].qf_start = qfp;
  1403.     --qf_lists[idx].qf_count;
  1404.     }
  1405. }
  1406.  
  1407. /*
  1408.  * qf_mark_adjust: adjust marks
  1409.  */
  1410.    void
  1411. qf_mark_adjust(line1, line2, amount, amount_after)
  1412.     linenr_T    line1;
  1413.     linenr_T    line2;
  1414.     long    amount;
  1415.     long    amount_after;
  1416. {
  1417.     int            i;
  1418.     struct qf_line    *qfp;
  1419.     int            idx;
  1420.  
  1421.     for (idx = 0; idx < qf_listcount; ++idx)
  1422.     if (qf_lists[idx].qf_count)
  1423.         for (i = 0, qfp = qf_lists[idx].qf_start;
  1424.                i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
  1425.         if (qfp->qf_fnum == curbuf->b_fnum)
  1426.         {
  1427.             if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
  1428.             {
  1429.             if (amount == MAXLNUM)
  1430.                 qfp->qf_cleared = TRUE;
  1431.             else
  1432.                 qfp->qf_lnum += amount;
  1433.             }
  1434.             else if (amount_after && qfp->qf_lnum > line2)
  1435.             qfp->qf_lnum += amount_after;
  1436.         }
  1437. }
  1438.  
  1439. /*
  1440.  * Make a nice message out of the error character and the error number:
  1441.  *  char    number    message
  1442.  *  e or E    0        " error"
  1443.  *  w or W    0        " warning"
  1444.  *  i or I    0        " info"
  1445.  *  0          0        ""
  1446.  *  other     0        " c"
  1447.  *  e or E    n        " error n"
  1448.  *  w or W    n        " warning n"
  1449.  *  i or I    n        " info n"
  1450.  *  0          n        " error n"
  1451.  *  other     n        " c n"
  1452.  */
  1453.     static char_u *
  1454. qf_types(c, nr)
  1455.     int c, nr;
  1456. {
  1457.     static char_u    buf[20];
  1458.     static char_u    cc[3];
  1459.     char_u        *p;
  1460.  
  1461.     if (c == 'W' || c == 'w')
  1462.     p = (char_u *)" warning";
  1463.     else if (c == 'I' || c == 'i')
  1464.     p = (char_u *)" info";
  1465.     else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
  1466.     p = (char_u *)" error";
  1467.     else if (c == 0)
  1468.     p = (char_u *)"";
  1469.     else
  1470.     {
  1471.     cc[0] = ' ';
  1472.     cc[1] = c;
  1473.     cc[2] = NUL;
  1474.     p = cc;
  1475.     }
  1476.  
  1477.     if (nr <= 0)
  1478.     return p;
  1479.  
  1480.     sprintf((char *)buf, "%s %3d", p, nr);
  1481.     return buf;
  1482. }
  1483.  
  1484. #if defined(FEAT_WINDOWS) || defined(PROTO)
  1485. /*
  1486.  * ":cwindow": open the quickfix window if we have errors to display,
  1487.  *           close it if not.
  1488.  */
  1489.     void
  1490. ex_cwindow(eap)
  1491.     exarg_T    *eap;
  1492. {
  1493.     win_T    *win;
  1494.  
  1495.     /*
  1496.      * Look for an existing quickfix window.
  1497.      */
  1498.     for (win = firstwin; win != NULL; win = win->w_next)
  1499.     if (bt_quickfix(win->w_buffer))
  1500.         break;
  1501.  
  1502.     /*
  1503.      * If a quickfix window is open but we have no errors to display,
  1504.      * close the window.  If a quickfix window is not open, then open
  1505.      * it if we have errors; otherwise, leave it closed.
  1506.      */
  1507.     if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
  1508.     {
  1509.     if (win != NULL)
  1510.         ex_cclose(eap);
  1511.     }
  1512.     else if (win == NULL)
  1513.     ex_copen(eap);
  1514. }
  1515.  
  1516. /*
  1517.  * ":cclose": close the window showing the list of errors.
  1518.  */
  1519. /*ARGSUSED*/
  1520.     void
  1521. ex_cclose(eap)
  1522.     exarg_T    *eap;
  1523. {
  1524.     win_T    *win;
  1525.  
  1526.     /*
  1527.      * Find existing quickfix window and close it.
  1528.      */
  1529.     for (win = firstwin; win != NULL; win = win->w_next)
  1530.     if (bt_quickfix(win->w_buffer))
  1531.         break;
  1532.  
  1533.     if (win != NULL)
  1534.     win_close(win, FALSE);
  1535. }
  1536.  
  1537. /*
  1538.  * ":copen": open a window that shows the list of errors.
  1539.  */
  1540.     void
  1541. ex_copen(eap)
  1542.     exarg_T    *eap;
  1543. {
  1544.     int        height;
  1545.     buf_T    *buf;
  1546.     win_T    *win;
  1547.  
  1548.     if (eap->addr_count != 0)
  1549.     height = eap->line2;
  1550.     else
  1551.     height = QF_WINHEIGHT;
  1552.  
  1553. #ifdef FEAT_VISUAL
  1554.     reset_VIsual_and_resel();            /* stop Visual mode */
  1555. #endif
  1556. #ifdef FEAT_GUI
  1557.     need_mouse_correct = TRUE;
  1558. #endif
  1559.  
  1560.     /*
  1561.      * Find existing quickfix window, or open a new one.
  1562.      */
  1563.     for (win = firstwin; win != NULL; win = win->w_next)
  1564.     if (bt_quickfix(win->w_buffer))
  1565.         break;
  1566.     if (win != NULL)
  1567.     win_goto(win);
  1568.     else
  1569.     {
  1570.     /* Create the new window at the very bottom. */
  1571.     win_goto(lastwin);
  1572.     if (win_split(height, WSP_BELOW) == FAIL)
  1573.         return;        /* not enough room for window */
  1574.  
  1575.     /*
  1576.      * Find existing quickfix buffer, or create a new one.
  1577.      */
  1578.     buf = qf_find_buf();
  1579.     if (buf == NULL)
  1580.     {
  1581.         (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
  1582.         /* switch off 'swapfile' */
  1583.         set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
  1584.         set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
  1585.                                    OPT_LOCAL);
  1586.         set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
  1587.         set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
  1588.     }
  1589.     else if (buf != curbuf)
  1590.         set_curbuf(buf, DOBUF_GOTO);
  1591.  
  1592.     /* Only set the height when there is no window to the side. */
  1593.     if (curwin->w_width == Columns)
  1594.         win_setheight(height);
  1595.     }
  1596.  
  1597.     /*
  1598.      * Fill the buffer with the quickfix list.
  1599.      */
  1600.     qf_fill_buffer();
  1601.  
  1602.     curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
  1603.     curwin->w_cursor.col = 0;
  1604.     check_cursor();
  1605.     update_topline();        /* scroll to show the line */
  1606. }
  1607.  
  1608. /*
  1609.  * Return the number of the current entry (line number in the quickfix
  1610.  * window).
  1611.  */
  1612.      linenr_T
  1613. qf_current_entry()
  1614. {
  1615.     return qf_lists[qf_curlist].qf_index;
  1616. }
  1617.  
  1618. /*
  1619.  * Update the cursor position in the quickfix window to the current error.
  1620.  * Return TRUE if there is a quickfix window.
  1621.  */
  1622.     static int
  1623. qf_win_pos_update(old_qf_index)
  1624.     int        old_qf_index;    /* previous qf_index or zero */
  1625. {
  1626.     win_T    *win;
  1627.     int        qf_index = qf_lists[qf_curlist].qf_index;
  1628.  
  1629.     /*
  1630.      * Put the cursor on the current error in the quickfix window, so that
  1631.      * it's viewable.
  1632.      */
  1633.     for (win = firstwin; win != NULL; win = win->w_next)
  1634.     if (bt_quickfix(win->w_buffer))
  1635.         break;
  1636.     if (win != NULL
  1637.         && qf_index <= win->w_buffer->b_ml.ml_line_count
  1638.         && old_qf_index != qf_index)
  1639.     {
  1640.     win_T    *old_curwin = curwin;
  1641.  
  1642.     curwin = win;
  1643.     curbuf = win->w_buffer;
  1644.     if (qf_index > old_qf_index)
  1645.     {
  1646.         curwin->w_redraw_top = old_qf_index;
  1647.         curwin->w_redraw_bot = qf_index;
  1648.     }
  1649.     else
  1650.     {
  1651.         curwin->w_redraw_top = qf_index;
  1652.         curwin->w_redraw_bot = old_qf_index;
  1653.     }
  1654.     curwin->w_cursor.lnum = qf_index;
  1655.     curwin->w_cursor.col = 0;
  1656.     update_topline();        /* scroll to show the line */
  1657.     redraw_later(VALID);
  1658.     curwin->w_redr_status = TRUE;    /* update ruler */
  1659.     curwin = old_curwin;
  1660.     curbuf = curwin->w_buffer;
  1661.     }
  1662.     return win != NULL;
  1663. }
  1664.  
  1665. /*
  1666.  * Find quickfix buffer.
  1667.  */
  1668.     static buf_T *
  1669. qf_find_buf()
  1670. {
  1671.     buf_T    *buf;
  1672.  
  1673.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1674.     if (bt_quickfix(buf))
  1675.         break;
  1676.     return buf;
  1677. }
  1678.  
  1679. /*
  1680.  * Find the quickfix buffer.  If it exists, update the contents.
  1681.  */
  1682.     static void
  1683. qf_update_buffer()
  1684. {
  1685.     buf_T    *buf;
  1686. #ifdef FEAT_AUTOCMD
  1687.     aco_save_T    aco;
  1688. #else
  1689.     buf_T    *save_curbuf;
  1690. #endif
  1691.  
  1692.     /* Check if a buffer for the quickfix list exists.  Update it. */
  1693.     buf = qf_find_buf();
  1694.     if (buf != NULL)
  1695.     {
  1696. #ifdef FEAT_AUTOCMD
  1697.     /* set curwin/curbuf to buf and save a few things */
  1698.     aucmd_prepbuf(&aco, buf);
  1699. #else
  1700.     save_curbuf = curbuf;
  1701.     curbuf = buf;
  1702. #endif
  1703.  
  1704.     qf_fill_buffer();
  1705.  
  1706. #ifdef FEAT_AUTOCMD
  1707.     /* restore curwin/curbuf and a few other things */
  1708.     aucmd_restbuf(&aco);
  1709. #else
  1710.     curbuf = save_curbuf;
  1711. #endif
  1712.  
  1713.     (void)qf_win_pos_update(0);
  1714.     }
  1715. }
  1716.  
  1717. /*
  1718.  * Fill current buffer with quickfix errors, replacing any previous contents.
  1719.  * curbuf must be the quickfix buffer!
  1720.  */
  1721.     static void
  1722. qf_fill_buffer()
  1723. {
  1724.     linenr_T        lnum;
  1725.     struct qf_line    *qfp;
  1726.     buf_T        *errbuf;
  1727.     int            len;
  1728.     int            old_KeyTyped = KeyTyped;
  1729.  
  1730.     /* delete all existing lines */
  1731.     while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
  1732.     (void)ml_delete((linenr_T)1, FALSE);
  1733.  
  1734.     /* Check if there is anything to display */
  1735.     if (qf_curlist < qf_listcount)
  1736.     {
  1737.     /* Add one line for each error */
  1738.     qfp = qf_lists[qf_curlist].qf_start;
  1739.     for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
  1740.     {
  1741.         if (qfp->qf_fnum != 0
  1742.             && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
  1743.             && errbuf->b_fname != NULL)
  1744.         {
  1745.         STRCPY(IObuff, errbuf->b_fname);
  1746.         len = (int)STRLEN(IObuff);
  1747.         }
  1748.         else
  1749.         len = 0;
  1750.         IObuff[len++] = '|';
  1751.  
  1752.         if (qfp->qf_lnum > 0)
  1753.         {
  1754.         sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
  1755.         len += (int)STRLEN(IObuff + len);
  1756.  
  1757.         if (qfp->qf_col > 0)
  1758.         {
  1759.             sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
  1760.             len += (int)STRLEN(IObuff + len);
  1761.         }
  1762.  
  1763.         sprintf((char *)IObuff + len, "%s",
  1764.             qf_types(qfp->qf_type, qfp->qf_nr));
  1765.         len += (int)STRLEN(IObuff + len);
  1766.         }
  1767.         IObuff[len++] = '|';
  1768.         IObuff[len++] = ' ';
  1769.  
  1770.         qf_fmt_text(qfp->qf_text, IObuff + len, IOSIZE - len);
  1771.  
  1772.         if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
  1773.                                       == FAIL)
  1774.         break;
  1775.         qfp = qfp->qf_next;
  1776.     }
  1777.     /* Delete the empty line which is now at the end */
  1778.     (void)ml_delete(lnum + 1, FALSE);
  1779.     }
  1780.  
  1781.     /* correct cursor position */
  1782.     check_lnums(TRUE);
  1783.  
  1784.     /* Set the 'filetype' to "qf" each time after filling the buffer.  This
  1785.      * resembles reading a file into a buffer, it's more logical when using
  1786.      * autocommands. */
  1787.     set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
  1788.     curbuf->b_p_ma = FALSE;
  1789.  
  1790. #ifdef FEAT_AUTOCMD
  1791.     apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
  1792.                                    FALSE, curbuf);
  1793.     apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
  1794.                                    FALSE, curbuf);
  1795. #endif
  1796.  
  1797.     /* make sure it will be redrawn */
  1798.     redraw_curbuf_later(NOT_VALID);
  1799.  
  1800.     /* Restore KeyTyped, setting 'filetype' may reset it. */
  1801.     KeyTyped = old_KeyTyped;
  1802. }
  1803.  
  1804. #endif /* FEAT_WINDOWS */
  1805.  
  1806. /*
  1807.  * Return TRUE if "buf" is the quickfix buffer.
  1808.  */
  1809.     int
  1810. bt_quickfix(buf)
  1811.     buf_T    *buf;
  1812. {
  1813.     return (buf->b_p_bt[0] == 'q');
  1814. }
  1815.  
  1816. /*
  1817.  * Return TRUE if "buf" is a "nofile" buffer.
  1818.  */
  1819.     int
  1820. bt_nofile(buf)
  1821.     buf_T    *buf;
  1822. {
  1823.     return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f');
  1824. }
  1825.  
  1826. /*
  1827.  * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
  1828.  */
  1829.     int
  1830. bt_dontwrite(buf)
  1831.     buf_T    *buf;
  1832. {
  1833.     return (buf->b_p_bt[0] == 'n');
  1834. }
  1835.  
  1836.     int
  1837. bt_dontwrite_msg(buf)
  1838.     buf_T    *buf;
  1839. {
  1840.     if (bt_dontwrite(buf))
  1841.     {
  1842.     EMSG(_("E382: Cannot write, 'buftype' option is set"));
  1843.     return TRUE;
  1844.     }
  1845.     return FALSE;
  1846. }
  1847.  
  1848. /*
  1849.  * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
  1850.  * and 'bufhidden'.
  1851.  */
  1852.     int
  1853. buf_hide(buf)
  1854.     buf_T    *buf;
  1855. {
  1856.     /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
  1857.     switch (buf->b_p_bh[0])
  1858.     {
  1859.     case 'u':            /* "unload" */
  1860.     case 'd': return FALSE;        /* "delete" */
  1861.     case 'h': return TRUE;        /* "hide" */
  1862.     }
  1863.     return (p_hid || cmdmod.hide);
  1864. }
  1865.  
  1866. /*
  1867.  * Used for ":make", ":grep" and ":grepadd".
  1868.  */
  1869.     void
  1870. ex_make(eap)
  1871.     exarg_T    *eap;
  1872. {
  1873.     char_u    *name;
  1874.     char_u    *cmd;
  1875.     unsigned    len;
  1876.  
  1877.     autowrite_all();
  1878.     name = get_mef_name();
  1879.     if (name == NULL)
  1880.     return;
  1881.     mch_remove(name);        /* in case it's not unique */
  1882.  
  1883.     /*
  1884.      * If 'shellpipe' empty: don't redirect to 'errorfile'.
  1885.      */
  1886.     len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
  1887.     if (*p_sp != NUL)
  1888.     len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3;
  1889.     cmd = alloc(len);
  1890.     if (cmd == NULL)
  1891.     return;
  1892.     sprintf((char *)cmd, "%s%s%s", p_shq, eap->arg, p_shq);
  1893.     if (*p_sp != NUL)
  1894.     append_redir(cmd, p_sp, name);
  1895.     /*
  1896.      * Output a newline if there's something else than the :make command that
  1897.      * was typed (in which case the cursor is in column 0).
  1898.      */
  1899.     if (msg_col == 0)
  1900.     msg_didout = FALSE;
  1901.     msg_start();
  1902.     MSG_PUTS(":!");
  1903.     msg_outtrans(cmd);        /* show what we are doing */
  1904.  
  1905.     /* let the shell know if we are redirecting output or not */
  1906.     do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
  1907.  
  1908. #ifdef AMIGA
  1909.     out_flush();
  1910.         /* read window status report and redraw before message */
  1911.     (void)char_avail();
  1912. #endif
  1913.  
  1914.     if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm,
  1915.                           eap->cmdidx != CMD_grepadd) > 0
  1916.         && !eap->forceit)
  1917.     qf_jump(0, 0, FALSE);        /* display first error */
  1918.  
  1919.     mch_remove(name);
  1920.     vim_free(name);
  1921.     vim_free(cmd);
  1922. }
  1923.  
  1924. /*
  1925.  * Return the name for the errorfile, in allocated memory.
  1926.  * Find a new unique name when 'makeef' contains "##".
  1927.  * Returns NULL for error.
  1928.  */
  1929.     static char_u *
  1930. get_mef_name()
  1931. {
  1932.     char_u    *p;
  1933.     char_u    *name;
  1934.     static int    start = -1;
  1935.     static int    off = 0;
  1936. #ifdef HAVE_LSTAT
  1937.     struct stat    sb;
  1938. #endif
  1939.  
  1940.     if (*p_mef == NUL)
  1941.     {
  1942.     name = vim_tempname('e');
  1943.     if (name == NULL)
  1944.         EMSG(_(e_notmp));
  1945.     return name;
  1946.     }
  1947.  
  1948.     for (p = p_mef; *p; ++p)
  1949.     if (p[0] == '#' && p[1] == '#')
  1950.         break;
  1951.  
  1952.     if (*p == NUL)
  1953.     return vim_strsave(p_mef);
  1954.  
  1955.     /* Keep trying until the name doesn't exist yet. */
  1956.     for (;;)
  1957.     {
  1958.     if (start == -1)
  1959.         start = mch_get_pid();
  1960.     else
  1961.         off += 19;
  1962.  
  1963.     name = alloc((unsigned)STRLEN(p_mef) + 30);
  1964.     if (name == NULL)
  1965.         break;
  1966.     STRCPY(name, p_mef);
  1967.     sprintf((char *)name + (p - p_mef), "%d%d", start, off);
  1968.     STRCAT(name, p + 2);
  1969.     if (mch_getperm(name) < 0
  1970. #ifdef HAVE_LSTAT
  1971.             /* Don't accept a symbolic link, its a security risk. */
  1972.             && mch_lstat((char *)name, &sb) < 0
  1973. #endif
  1974.         )
  1975.         break;
  1976.     vim_free(name);
  1977.     }
  1978.     return name;
  1979. }
  1980.  
  1981. /*
  1982.  * ":cc", ":crewind", ":cfirst" and ":clast".
  1983.  */
  1984.     void
  1985. ex_cc(eap)
  1986.     exarg_T    *eap;
  1987. {
  1988.     qf_jump(0,
  1989.         eap->addr_count > 0
  1990.         ? (int)eap->line2
  1991.         : eap->cmdidx == CMD_cc
  1992.         ? 0
  1993.         : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
  1994.             ? 1
  1995.             : 32767,
  1996.         eap->forceit);
  1997. }
  1998.  
  1999. /*
  2000.  * ":cnext", ":cnfile", ":cNext" and ":cprevious".
  2001.  */
  2002.     void
  2003. ex_cnext(eap)
  2004.     exarg_T    *eap;
  2005. {
  2006.     qf_jump(eap->cmdidx == CMD_cnext
  2007.         ? FORWARD
  2008.         : eap->cmdidx == CMD_cnfile
  2009.         ? FORWARD_FILE
  2010.         : BACKWARD,
  2011.         eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
  2012. }
  2013.  
  2014. /*
  2015.  * ":cfile" command.
  2016.  */
  2017.     void
  2018. ex_cfile(eap)
  2019.     exarg_T    *eap;
  2020. {
  2021.     if (*eap->arg != NUL)
  2022.     set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
  2023.     if (qf_init(p_ef, p_efm, TRUE) > 0)
  2024.     qf_jump(0, 0, eap->forceit);        /* display first error */
  2025. }
  2026.  
  2027. #endif /* FEAT_QUICKFIX */
  2028.